<?php

namespace App\Services;

use App\Helpers\Tools;
use App\Models\Works;
use App\Models\WorksZan;
use EasyWeChat\Factory;

class WeChatService {

    protected $config;
    protected $jssdkConfig;

    public function __construct()
    {
        $this->config = [
            'app_id' => 'wxa37469b97aae05bd',
            'secret' => '79ccdb72e8be76b120b541d05e46a75b',

            'oauth' => [
                'scopes'   => ['snsapi_userinfo'],
                'callback' => 'https://cooperate.chenrilong.top/wechat/callback',
            ],
            'response_type' => 'array',

            'log' => [
                'level' => 'debug',
                'file' => __DIR__.'/../../storage/logs/wechat.log',
            ],
        ];

        $this->jssdkConfig = [
            'app_id' => 'wxa37469b97aae05bd',
            'secret' => '79ccdb72e8be76b120b541d05e46a75b',

            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',

            'log' => [
                'level' => 'debug',
                'file' => __DIR__.'/../../storage/logs/wechat.log',
            ],
        ];


    }

    public function oauth($type = 1)
    {
        // TODO 貌似无用，但不删
        if($type == 2) {
            $this->config['oauth']['callback'] = $this->config['oauth']['callback'] . '?type=2';
        }

        // 人脸融合
        if($type == 3) {
            $this->config['oauth']['callback'] = $this->config['oauth']['callback'] . '?type=3';
        }

        $app = Factory::officialAccount($this->config);
        $oauth = $app->oauth;

        return $oauth;
    }

    public function jssdkJson($url)
    {
        $app = Factory::officialAccount($this->config);

        $config = $app->jssdk->setUrl($url)->buildConfig(array("hideMenuItems", "checkJsApi", "openLocation", "getLocation", "onMenuShareTimeline", "onMenuShareAppMessage", "menuItem:share:qq", "menuItem:openWithSafari"), true);

        return $config;
    }

    public function addCard()
    {
        $cards = [
            ['card_id' => 'pKcVMwPoZ_lSfTf2gihf5WgjyPYM'],
        ];

        $app = Factory::officialAccount($this->jssdkConfig);
        $card = $app->card;

        $json = $card->jssdk->assign($cards);

        return $json;

    }
}